﻿namespace QMVnSoft.DAL
{
    using System;
    using System.Data;
    using System.Data.SqlClient;

    public class DmTkDataAccess
    {
        public DataSet GetDataSet()
        {
            DataSet set2;
            SqlConnection eZConnection = AppDataAccess.AppConnection;
            DataSet dataSet = new DataSet();
            SqlDataAdapter adapter = new SqlDataAdapter();
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "DmTk_Get";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = eZConnection;
                adapter.SelectCommand = command;
                adapter.Fill(dataSet);
                AppDataAccess.CloseConnection(eZConnection);
                set2 = dataSet;
            }
            catch (Exception exception)
            {
                throw new NotSupportedException("Invalid expression", exception);
            }
            return set2;
        }

        public DataSet GetDataSet(string ps_UserName)
        {
            DataSet set2;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_UserName", ps_UserName) };
            SqlConnection eZConnection = AppDataAccess.AppConnection;
            DataSet dataSet = new DataSet();
            SqlDataAdapter adapter = new SqlDataAdapter();
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "DmTk_Get";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = eZConnection;
                command.Parameters.AddRange(values);
                adapter.SelectCommand = command;
                adapter.Fill(dataSet);
                AppDataAccess.CloseConnection(eZConnection);
                set2 = dataSet;
            }
            catch (Exception exception)
            {
                throw new NotSupportedException("Invalid expression", exception);
            }
            return set2;
        }

        public DataSet GetDataSet(string ps_UserName, string ps_Code)
        {
            DataSet set2;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_UserName", ps_UserName), new SqlParameter("p_Tk", ps_Code) };
            SqlConnection eZConnection = AppDataAccess.AppConnection;
            DataSet dataSet = new DataSet();
            SqlDataAdapter adapter = new SqlDataAdapter();
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "DmTk_Get";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = eZConnection;
                command.Parameters.AddRange(values);
                adapter.SelectCommand = command;
                adapter.Fill(dataSet);
                AppDataAccess.CloseConnection(eZConnection);
                set2 = dataSet;
            }
            catch (Exception exception)
            {
                throw new NotSupportedException("Invalid expression", exception);
            }
            return set2;
        }

        public int SaveRow(bool pb_NewChecked, DataRow row)
        {
            int num;
            SqlParameter[] values = new SqlParameter[] { 
                new SqlParameter("p_Moi_Sua", pb_NewChecked), new SqlParameter("p_Stt", row["Stt"]), new SqlParameter("p_Tk", row["Tk"]), new SqlParameter("p_Ten_Tk", row["Ten_Tk"]), new SqlParameter("p_Ten_Tk_E", row["Ten_Tk_E"]), new SqlParameter("p_Tk_NT", row["Tk_NT"]), new SqlParameter("p_Tk_BP", row["Tk_BP"]), new SqlParameter("p_Tk_HD", row["Tk_HD"]), new SqlParameter("p_Tk_DT", row["Tk_DT"]), new SqlParameter("p_Tk_KV", row["Tk_KV"]), new SqlParameter("p_Tk_GT", row["Tk_GT"]), new SqlParameter("p_Tk_SC", row["Tk_SC"]), new SqlParameter("p_Tk_KM", row["Tk_KM"]), new SqlParameter("p_Tk_VAT", row["Tk_VAT"]), new SqlParameter("p_Tk_Me", row["Tk_Me"]), new SqlParameter("p_Ma_Loai1", row["Ma_Loai1"]), 
                new SqlParameter("p_Ma_Loai2", row["Ma_Loai2"]), new SqlParameter("p_Ma_Loai3", row["Ma_Loai3"]), new SqlParameter("p_Result", SqlDbType.BigInt)
             };
            values[1].Direction = ParameterDirection.InputOutput;
            values[1].SqlDbType = SqlDbType.BigInt;
            values[0x12].Direction = ParameterDirection.Output;
            values[0x12].SqlDbType = SqlDbType.Int;
            SqlConnection eZConnection = AppDataAccess.AppConnection;
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "DmTk_Save";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = eZConnection;
                command.Parameters.AddRange(values);
                command.ExecuteNonQuery();
                AppDataAccess.CloseConnection(eZConnection);
                row["Stt"] = values[1].Value;
                num = (int) values[0x12].Value;
            }
            catch (Exception exception)
            {
                throw new NotSupportedException("Invalid expression", exception);
            }
            return num;
        }
    }
}

